package kr.co.handysoft.smartPolice.server;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.zip.DataFormatException;

import kr.co.handysoft.common.compress.Archiving;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Worker implements Runnable {

	private String data;
	private String zone;
	private String kor;

	public Worker(String data, String zone, String kor) {
		this.data = data;
		this.zone = zone;
		this.kor = kor;
	}

	@Override
	public void run() {
		String recvString;
		try {
			recvString = new String(Archiving.decompress(Archiving.hexToByteArray(data)));
			JSONObject recvJSONObj = JSONObject.fromObject(recvString);
			JSONArray jsonArray = recvJSONObj.getJSONArray("files");
			int arraySize = jsonArray.size();
			
			for (int i=0; i<arraySize; i++){
				JSONObject jobj = jsonArray.getJSONObject(i);
				String fileName = jobj.getString("date") + "_" + jobj.getString("site") + "_" + jobj.getString("line") + "_" + jobj.getString("number") + "_" + jobj.getString("speed");
				File file = new File("C:/GFTP/to4Dream/" + fileName + ".txt");
				Writer out = new OutputStreamWriter(new FileOutputStream(file));
				out.write("\n\n" + this.zone + "\n" + this.kor + "\n\n\n\n" + jobj.getString("ftpPath") + "\n\n\n\n\n\n\n\n\n");
				out.flush();
				out.close();
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (DataFormatException e) {
			e.printStackTrace();
		}
	}

}
